package com.tianwen.management.controller;

import com.tianwen.management.entity.Department;
import com.tianwen.management.entity.Role;
import com.tianwen.management.entity.User;
import com.tianwen.management.result.Result;
import com.tianwen.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

/**
 * @program: personnel-management
 * @description
 * @author: tianwen
 * @create: 2023-05-17 09:05
 **/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("/findAll")
    public Result findAll(){
        List<Role> roleList = roleService.list();
        return Result.ok().data("roleList",roleList);
    }
    @GetMapping("/page/{page}/{pageSize}")
    public Result page(@PathVariable Integer page, @PathVariable Integer pageSize, String name){
        List<Role> roleList=roleService.page(page, pageSize, name);
        return Result.ok().data("roleList",roleList);
    }
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('角色增删改')")
    public Result add(@RequestBody Role role){
        if(roleService.saveAndPermission(role)){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('角色增删改')")
    public Result delete(Long id){
        if(roleService.removeAndPermission(id)){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('角色增删改')")
    public Result update(@RequestBody Role role){
        if(roleService.updateAndPermission(role)){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}
